@echo off

REM TCP2MQTT Gateway GUI Launcher
REM Windows platform only

echo =======================================
echo       TCP2MQTT Gateway GUI Launcher
 echo =======================================

REM Set working directory to script location
cd /d %~dp0

echo Working directory: %cd%

REM Check if Python is installed
python --version >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: Python not found. Please install Python 3.6 or higher.
    pause
    exit /b 1
)

REM Check if virtual environment exists
if not exist "venv" (
    echo Virtual environment not found, creating...
    python -m venv venv
    
    REM Activate virtual environment
    call venv\Scripts\activate
    
    REM Upgrade pip
    echo Upgrading pip...
    pip install --upgrade pip
    
    REM Install dependencies
    echo Installing dependencies...
    pip install -r requirements.txt
    
    REM Check installation success
    if %errorlevel% neq 0 (
        echo Error: Failed to install dependencies.
        pause
        exit /b 1
    )
    
    echo Dependencies installed.
) else (
    REM Activate existing virtual environment
    call venv\Scripts\activate
    
    REM Check for dependency updates
    echo Checking for dependency updates...
    pip install -r requirements.txt --upgrade
)

REM Start GUI
echo Starting TCP2MQTT Gateway GUI...
python tcp2mqtt_gui.py

REM Check if start was successful
if %errorlevel% neq 0 (
    echo Error: Failed to start GUI.
    pause
    exit /b 1
)

REM Program exit
echo Program exited.
pause